<template>
  <a-modal :title="title" :after-close="reset" @ok="submit">
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="{ style: { width: '80px' } }"
    >
      <a-form-item label="月结账号" name="account">
        <a-input v-model:value="form.account" placeholder="账号" />
      </a-form-item>
      <a-form-item label="月结密码" name="password">
        <a-input v-model:value="form.password" placeholder="密码" />
      </a-form-item>
      <a-form-item label="网点名称" name="netName">
        <a-input v-model:value="form.netName" placeholder="网点名称" />
      </a-form-item>
      <a-form-item label="排序" name="sort">
        <a-input-number
          v-model:value="form.sort"
          :min="0"
          :max="999"
          placeholder="排序"
        />
      </a-form-item>
      <a-form-item label="是否启用" name="status">
        <a-radio-group v-model:value="form.status">
          <a-radio :value="true"> 开启 </a-radio>
          <a-radio :value="false"> 关闭 </a-radio>
        </a-radio-group>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts">
import { expressAddup, expressinfo } from './api';
import { express, type Iexpress } from './entity';
import { validReg } from '@/utils/validate';

const props = withDefaults(defineProps<{
  data: Partial<Iexpress>
}>(),{
})
const title = computed(() => props.data.id ? '编辑' : '新增')
const { formRef, submit, close } = useDialog({ validCallback: save })

let form = $ref(new express())
watchEffect(() => {
  if (props.data.id) {
    expressinfo(props.data.id).then(res => {
      form.setProps(res)
    })
  }
})

const rules = {
        expressName: [validReg('required', '请输入用户名')]
}

onMounted(() => {
})
const emit = defineEmits(['change'])
function save() {
    expressAddup(form).then(() => {
    AntMessage.success('保存成功')
    close()
    emit('change')
  })
}

function reset() {
  form = new express()
}
</script>

<style lang="less"></style>
